<?php

//category
function listAllNewsCategory($parent_id = 0,$space = ' ', $trees = NULL) {
    global $db;
    if(!$trees) $trees = array();
    $rsCat = $db->query("SELECT * FROM cat_news WHERE cat_parent = ".intval($parent_id)." AND cat_active = 'yes' AND lang='".$_SESSION["lang"]."' ORDER BY cat_order,cat_id");
    while($rs=$db->fetch_array($rsCat)) {
        if($rs['cat_parent']==0) {
            $trees[] = array("cat_id"=>$rs['cat_id'],"cat_title"=>$rs['cat_title'],"cat_link"=>$rs['cat_link']);
        }else {
            $trees[] = array("cat_id"=>$rs['cat_id'],"cat_title"=>$space.$rs['cat_title'],"cat_link"=>$rs['cat_link']);
        }
        $trees = listAllNewsCategory($rs['cat_id'],$space.'----',$trees);
    }
    return $trees;
}

function getCatNewsIdToArray($parent_id = 0,$trees=null) {
    global $db;
    if(!$trees) $trees = array();
    $rsCat = $db->query("SELECT cat_id FROM cat_news WHERE  cat_parent = ".intval($parent_id));
    if($rsCat) {
        while($rs = $db->fetch_array($rsCat)) {
            $trees[] = $rs['cat_id'];
            $trees = getCatNewsIdToArray($rs['cat_id'],$trees);
        }
    }
    return $trees;
}

function getCategoryByLink($cat_link) {
    global $db;
    $rs = $db->query_first("SELECT cat_id,cat_title,cat_link,cat_active FROM cat_news WHERE cat_link ='".mysql_real_escape_string($cat_link)."'" );
    return $rs;
}

function getCategoryById($cat_id) {
    global $db;
    $rs = $db->query_first("SELECT cat_id,cat_title,cat_link,cat_active FROM cat_news WHERE cat_id =$cat_id");
    return $rs;
}
//end category


//tags

function getTagsByLink($tags_link) {
    global $db;
    $rs = $db->query_first("SELECT id,tags,tags_link FROM tags_news WHERE tags_link ='".mysql_real_escape_string($tags_link)."'");
    return $rs;
}

function getNewsByTagsWithPaging($tags_id=0,$page=1) {
    global $db,$CONFIG;

    $rsTotal=$db->query_first("SELECT COUNT(n.id) as total FROM news n
                        LEFT JOIN news_relate_tags nrt ON n.id=nrt.news_id
                        WHERE n.active='yes' AND n.type='normal' AND nrt.tag_id=$tags_id
                        ORDER BY n.create_date DESC");
    $page=intval($page) ? intval($page) :1;
    $limit  = $CONFIG['rows_per_page_news'];
    $total  =ceil($rsTotal['total']/$limit);
    $start  = (($page*$limit)-$limit);
    $news = $db->query("SELECT n.id,n.title,n.title_link,n.description,n.image,n.create_date
                        FROM news n
                        LEFT JOIN news_relate_tags nrt ON n.id=nrt.news_id
                        WHERE n.active='yes' AND n.type='normal' AND nrt.tag_id=$tags_id
                        ORDER BY n.create_date DESC LIMIT $start,$limit");

    return array("news"=>$news,"page"=>$page,"total"=>$total);
}


function getOrtherNewsByTags($tags_id,$newsCreateDate) {
    global $db,$CONFIG;
    //return $db->query("SELECT title, title_link, description, image,create_date FROM news WHERE active='yes' AND cat_id=$catId AND create_date<'$newsCreateDate' ORDER BY create_date DESC LIMIT ".$CONFIG['rows_per_page_news_other']);
    if($newsCreateDate!='') {
        return $db->query("SELECT n.id,n.title,n.title_link,n.description,n.image,n.create_date FROM news n
                        LEFT JOIN news_relate_tags nrt ON n.id=nrt.news_id
                        WHERE n.active='yes' AND n.create_date<'$newsCreateDate' AND nrt.tag_id=$tags_id
                        ORDER BY n.create_date DESC");
    }
}
//end tag

//begin archive
function getNewsArchiveWithPaging($month='',$page=1) {
    global $db,$CONFIG;
    $rsTotal=$db->query_first("SELECT COUNT(n.id) as total FROM news n
                        WHERE n.active='yes' AND n.type='normal' AND n.create_date LIKE '$month%'");
    $page=intval($page) ? intval($page) :1;
    $limit  = $CONFIG['rows_per_page_news'];
    $total  =ceil($rsTotal['total']/$limit);
    $start  = (($page*$limit)-$limit);
    $news = $db->query("SELECT n.id,n.title,n.title_link,n.description,n.image,n.create_date
                        FROM news n
                        WHERE n.active='yes' AND n.type='normal' AND n.create_date LIKE '$month%'
                        ORDER BY n.create_date DESC LIMIT $start,$limit");

    return array("news"=>$news,"page"=>$page,"total"=>$total);
}

function getOrtherNewsArchive($newsCreateDate) {
    global $db,$CONFIG;
    //return $db->query("SELECT title, title_link, description, image,create_date FROM news WHERE active='yes' AND cat_id=$catId AND create_date<'$newsCreateDate' ORDER BY create_date DESC LIMIT ".$CONFIG['rows_per_page_news_other']);
    if($newsCreateDate!='') {
        return $db->query("SELECT n.id,n.title,n.title_link,n.description,n.image,n.create_date FROM news n
                        WHERE n.active='yes' AND n.create_date<'$newsCreateDate'
                        ORDER BY n.create_date DESC");
    }
}
//end archive


//news
function getNewsByCatIdWithPaging($cat_id=0,$page=1) {
    global $db,$CONFIG;
    if(intval($cat_id)>0) {
        $arrCatId = getCatNewsIdToArray(intval($cat_id));
        array_push($arrCatId,intval($cat_id));
        $strCatId = implode(',', $arrCatId);
        $condition .= " AND cat_id IN (".$strCatId.")";
    }

    $sel_all=$db->query_first("SELECT COUNT(id) m FROM news WHERE 1".$condition);

    $page=intval($page) ? intval($page) :1;
    $page=intval($page);
    if ($page<0) $page=1;
    $number=$sel_all['m'];
    $limit = $CONFIG['rows_per_page_news'];
    $total=ceil($number/$limit);
    $start = (($page*$limit)-$limit);

    $news = $db->query("SELECT id,title, title_link, description, image,create_date FROM news WHERE active='yes' AND type='normal'".$condition." ORDER BY create_date DESC LIMIT $start,$limit");

    return array("news"=>$news,"page"=>$page,"total"=>$total);
}

function getNewestNews() {
    global $db,$CONFIG;
    return $db->query("SELECT title, title_link, description, image,create_date FROM news WHERE active='yes' ORDER BY create_date DESC LIMIT ".$CONFIG['rows_per_page_news_home']);
}

function getOrtherNewsByCat($catId,$newsCreateDate) {
    global $db,$CONFIG;
    if($newsCreateDate!='') {
        return $db->query("SELECT title, title_link, description, image,create_date
                            FROM news
                            WHERE active='yes' AND cat_id=$catId AND create_date<'$newsCreateDate'
                            ORDER BY create_date DESC LIMIT ".$CONFIG['rows_per_page_news_other']);
    }
}

function getLeftTag() {
    global $db,$CONFIG;
    return $db->query("SELECT tags,tags_link FROM tags_news WHERE 1 ORDER BY word_count DESC LIMIT ".$CONFIG['num_of_tags_on_left_news']);
};

//function getNewsFocus($catId = 0){
//    global $db,$CONFIG;
//    if(intval($catId)){
//        $condition = " AND cat_id=$catId";
//    }else{
//        $condition = "";
//    }
//    $rsForcus = $db->query("SELECT title, title_link, description, image,create_date FROM news WHERE active='yes' $condition ORDER BY view_count DESC LIMIT ".$CONFIG['rows_per_page_news_focus']);
//    if(!$rsForcus){
//        $rsForcus = $db->query("SELECT title, title_link, description, image,create_date FROM news WHERE active='yes' ORDER BY view_count DESC LIMIT ".$CONFIG['rows_per_page_news_focus']);
//    }
//    return $rsForcus;
//}

function getNewsDetail($title_link='') {
    global $db;

    $db->query_first("UPDATE news SET view_count=view_count+1 WHERE title_link='".mysql_real_escape_string($title_link)."'");
    $rs = $db->query_first("SELECT id,cat_id,title,title_link,description,content,create_date FROM news WHERE title_link ='".mysql_real_escape_string($title_link)."'");

    if($rs['id']) {
        return $rs;
    }else {
        server_redirect(SITE_URL);
    }
}

function getCatInfo($catId){
    global $db;
    $rs = $db->query_first("SELECT cat_id,cat_title,cat_link,cat_active FROM cat_news WHERE cat_id=".$catId);
    return $rs;
}

function getNewsByCatHome($catId){
    global $db;
    $rs = $db->query_first("SELECT id, title, title_link, description, image,create_date FROM news WHERE cat_id=".$catId ." AND active='yes' AND lang='".$_SESSION["lang"]."' ORDER BY create_date DESC");
    $rsOther = $db->query("SELECT id, title, title_link, description, image,create_date FROM news WHERE cat_id=".$catId ." AND active='yes' AND lang='".$_SESSION["lang"]."' ORDER BY create_date DESC LIMIT 1,3");
    return array('rs'=>$rs,'rsOther'=>$rsOther);
}

function getNewsMostRead(){
    global $db;
    $rs = $db->query("SELECT id, title, title_link, description, image,create_date FROM news WHERE active='yes' AND type='normal' AND lang='".$_SESSION["lang"]."' ORDER BY view_count DESC LIMIT 0,10");
    return $rs;
}

function getNewsHotRight(){
    global $db;
    $rs = $db->query("SELECT id, title, title_link, description, image,create_date FROM news WHERE active='yes' AND type='hot' ORDER BY view_count,create_date DESC LIMIT 0,5");
    return $rs;
}

function getPropertyCatRight(){
    global $db;
    $rs = $db->query("SELECT p.title, p.title_link,p.thumb, p.description,p.price,p.create_date,cp.cat_title,cp.cat_link,g.geo_title,g.geo_link FROM property p LEFT JOIN cat_property cp ON p.cat_id=cp.cat_id RIGHT JOIN geographic g ON p.geo_id=g.geo_id WHERE status='actived' AND type='sell' ORDER BY view_count,create_date DESC LIMIT 0,5");
    return $rs;
}
//end news